import { ConfigEnv, UserConfigExport, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
import UnoCSS from "unocss/vite";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import { AntDesignVueResolver } from "unplugin-vue-components/resolvers";
import postcssPxToViewport from "postcss-px-to-viewport-8-plugin";
import basicSsl from "@vitejs/plugin-basic-ssl";

export default ({ mode }: ConfigEnv): UserConfigExport => {
	const viteEnv = loadEnv(mode, process.cwd());
	const { VITE_PUBLIC_PATH, VITE_APP_BASE_API } = viteEnv;

	console.log("=>   ", mode, viteEnv);
	return {
		/** 打包时根据实际情况修改 base ,资源路径前缀 */
		base: VITE_PUBLIC_PATH,
		plugins: [
			basicSsl(),
			vue(),
			UnoCSS(),
			AutoImport({
				resolvers: [ElementPlusResolver()],
			}),
			Components({
				resolvers: [
					ElementPlusResolver(),
					AntDesignVueResolver({
						importStyle: false, // css in js
					}),
				],
			}),
		],
		css: {
			postcss: {
				plugins: [
					postcssPxToViewport({
						unitToConvert: "px",
						viewportWidth: 1781,
						unitPrecision: 5,
						propList: ["*"],
						viewportUnit: "vw",
						fontViewportUnit: "vw",
						selectorBlackList: ["ignore-"],
						minPixelValue: 1,
						mediaQuery: false,
						replace: true,
						exclude: [/node_modules/],
						landscape: false,
					}),
				],
			},
		},
		resolve: {
			alias: {
				"@": path.resolve(__dirname, "src"), // 设置别名为 src 目录相对路径别名配置，使用 @ 代替 src
			},
		},
		server: {
			proxy: {
				"/api": {
					target: VITE_APP_BASE_API,
					changeOrigin: true,
					rewrite: (path) => path.replace(/^\/api/, ""),
				},
			},
			port: 7777,
		},
		// envDir: "./src/environment",
		build: {
			outDir: "icewantwant", // 制定输出路径
		},
		// basePath: "/icewantwant/",
	};
};
